package com.micookie.v2ex.api;

import com.micookie.v2ex.api.inf.ITopicRequest;
import com.micookie.v2ex.model.bean.Topic;
import com.orhanobut.logger.Logger;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/10/28.
 */

public class Request {
    private static final String TAG = "Request";
    private static Request request;


    public static synchronized Request getInstance() {
        if (request == null) {
            synchronized (Request.class) {
                if (request == null) {
                    request = new Request();
                }
            }
        }
        return request;
    }

    public void getTopicLatest(final Callback<List<Topic>> callback) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(APIConfig.getBaseUrl())
                .addConverterFactory(GsonConverterFactory.create()).build();
        ITopicRequest topicRequest = retrofit.create(ITopicRequest.class);
        Call<List<Topic>> call = topicRequest.getTopicLatest();
        call.enqueue(new Callback<List<Topic>>() {
            @Override
            public void onResponse(Call<List<Topic>> call, Response<List<Topic>> response) {
                Logger.d(TAG, response.toString());
                callback.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<List<Topic>> call, Throwable t) {
                callback.onFailure(call, t);
            }
        });
    }


//    public static void getTopicList(String url){
//
//        OkHttpClient client = new OkHttpClient();
//        okhttp3.Request
//
//        String run(String url) throws IOException {
//            Request request = new Request.Builder().url(url).build();
//            Response response = client.newCall(request).execute();    if (response.isSuccessful()) {        return response.body().string();
//            } else {        throw new IOException("Unexpected code " + response);
//            }
//        }
//    }
}
